package com.fanbuchi.fukua.activity.base;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.Toast;

import com.umeng.analytics.MobclickAgent;

import com.fanbuchi.fukua.application.SuperApp;
import com.fanbuchi.fukua.config.AppConfig;
import com.fanbuchi.fukua.entity.enums.Actions;
import com.fanbuchi.fukua.entity.enums.Categorys;
import com.fanbuchi.fukua.entity.event.DialogEvent;

import cn.jpush.android.api.JPushInterface;
import de.greenrobot.event.EventBus;

/**
 * Created by killerzhangsan on 2015/5/19.
 * @title 和业务无关的框架层封装
 * @title http封装
 * @title location
 * @title umeng
 */
public class SimpleActivity extends AppCompatActivity {

    IntentFilter intentFilter = new IntentFilter();
     Integer mCountDialog=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (AppConfig.DEBUG){
            MobclickAgent.setDebugMode(false);
        }else{
            MobclickAgent.setDebugMode(false);
        }
        //
        intentFilter.addAction(Actions.action_start_dialog);
        intentFilter.addAction(Actions.action_stop_dialog);
        intentFilter.addCategory(Categorys.category_dialog);

        EventBus.getDefault().register(this);
        //BusManager.getInstance().register(this);
    }

    /**
     * @title 同 通知实现类一样
     * @param dialogEvent
     */
    public void onEvent(DialogEvent dialogEvent) {
        if (dialogEvent.getAction()==Actions.action_start_dialog){
            synchronized (mCountDialog){
                mCountDialog++;
            }

            if (mCountDialog==1){
                display("开始开始开始");
            }

        }else if (dialogEvent.getAction()==Actions.action_stop_dialog){
            synchronized (mCountDialog){
                mCountDialog--;
            }

            if (mCountDialog<=0){
                mCountDialog=0;
                display("结束结束结束结束");
            }

        }

    }


    /*@Subscribe
    public void onBusEvent(DialogEvent dialogEvent) {
        if (dialogEvent.getAction()==Actions.action_start_dialog){
            synchronized (mCountDialog){
                mCountDialog++;
            }


            if (mCountDialog==1){
                display("开始开始开始");
            }

        }else if (dialogEvent.getAction()==Actions.action_stop_dialog){
            synchronized (mCountDialog){
                mCountDialog--;
            }

            if (mCountDialog<=0){
                mCountDialog=0;
                display("结束结束结束结束");
            }

        }

    }*/



    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        JPushInterface.onResume(this);
       //registerReceiver(mDialogReceiver, intentFilter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
        JPushInterface.onPause(this);
        if (SuperApp.getLocationManager().isLocationing()
                &&!SuperApp.getLocationManager().isBackgroundLocationing()){
            SuperApp.getLocationManager().stopMyLocation();
        }
       // unregisterReceiver(mDialogReceiver);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        //BusManager.getInstance().unregister(this);
    }

    BroadcastReceiver mDialogReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction()==Actions.action_start_dialog){
                    synchronized (mCountDialog){
                        mCountDialog++;
                    }

                if (mCountDialog==1){
                    display("开始开始开始");
                }

            }else if (intent.getAction()==Actions.action_stop_dialog){
                synchronized (mCountDialog){
                    mCountDialog--;
                }

                if (mCountDialog<=0){
                    mCountDialog=0;
                    display("结束结束结束结束");
                }

            }
        }
    };

    /**
     * @title 启动activity
     * @param clazz 启动的activityclass
     */
    public void startActivity(Class<? extends Activity> clazz) {
        Intent intent  = new Intent(this,clazz);
        startActivityForResult(intent,0);
    }

    /**
     * @title display magic
     * @param show 需要显示文字
     */
    public void display(String show){
        if (TextUtils.isEmpty(show)){
            return;
        }
        Toast.makeText(SimpleActivity.this,show,Toast.LENGTH_SHORT).show();
    }


    /**
     * @title display magic
     * @param show 需要显示文字ID
     */
    public void display(int show){
        if (show==0){
            return;
        }
        String _show=getResources().getString(show);
        display(_show);
    }


}
